package com.cmcc.aregister.cs.asntask;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.cmcc.aregister.cs.asntask.InterfaceUtils.DataCallback_cancel_other_order;
import com.cmcc.aregister.cs.asntask.InterfaceUtils.DataCallback_get_other_orders;
import com.cmcc.aregister.cs.asntask.InterfaceUtils.DataCallback_get_services;
import com.cmcc.aregister.cs.asntask.InterfaceUtils.DataCallback_other;
import com.cmcc.aregister.cs.asntask.InterfaceUtils.DataCallback_other_order;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.model.AreaModel;
import com.cmcc.aregister.cs.model.AreaResultModel;
import com.cmcc.aregister.cs.model.CandelOrderResultModel;
import com.cmcc.aregister.cs.model.DepModel;
import com.cmcc.aregister.cs.model.DepResultModel;
import com.cmcc.aregister.cs.model.DocInfoModel;
import com.cmcc.aregister.cs.model.DocModel;
import com.cmcc.aregister.cs.model.DocResultModel;
import com.cmcc.aregister.cs.model.GeneraOrderResultModel;
import com.cmcc.aregister.cs.model.GetServicesResultModel;
import com.cmcc.aregister.cs.model.HospitalInfoModel;
import com.cmcc.aregister.cs.model.HospitalModel;
import com.cmcc.aregister.cs.model.HospitalResultModel;
import com.cmcc.aregister.cs.model.OrderInfoModel;
import com.cmcc.aregister.cs.model.OrderListResultModel;
import com.cmcc.aregister.cs.model.OrderModel;
import com.cmcc.aregister.cs.model.ScheduleModel;
import com.cmcc.aregister.cs.model.ScheduleResultModel;
import com.cmcc.aregister.cs.model.ServiceInfoModel;

/**
 * Params 启动任务执行的输入参数，比如HTTP请求的URL。
   Progress 后台任务执行的百分比。
   Result 后台执行任务最终返回的结果，比如String。
 
 * @author luodan
 *
 */
public class MyAsynaTask extends AsyncTask<String, Integer, String> {
	
	Map<String, String> map;
	DataCallback_other mDataCallback;
	DataCallback_other_order mDataCallback_other_order;
	DataCallback_get_other_orders mDataCallback_get_other_orders;
	DataCallback_cancel_other_order mDataCallback_cancel_other_order;
	DataCallback_get_services mDataCallback_get_services;
	int code;
	private AreaResultModel mAreaResultModel;
	private HospitalResultModel mHospitalResultModel;
	private DepResultModel mDepResultModel;
	private DocResultModel mDocResultModel;
	private ScheduleResultModel mScheduleResultModel;
	
	private HospitalInfoModel mHospitalInfoModel;
	private DocInfoModel docInfoModel;
	
	private GeneraOrderResultModel orderResultModel;
	private OrderListResultModel orderListResultModel;
	private CandelOrderResultModel candelOrderResultModel;
	private OrderInfoModel orderInfoModel;
	private GetServicesResultModel getServicesResultModel;
	public void initMyAsynaTask(int code,Map<String, String> map){
		this.map = map;
		this.code = code;
	}
	public MyAsynaTask(){}
    public MyAsynaTask(DataCallback_other mDataCallback,int code,Map<String, String> map) {
		super();
		// TODO Auto-generated constructor stub
		initMyAsynaTask(code,map);
		this.mDataCallback = mDataCallback;
	}
    
    public MyAsynaTask(DataCallback_other_order mDataCallback,int code,Map<String, String> map) {
		super();
		// TODO Auto-generated constructor stub
		initMyAsynaTask(code,map);
		this.mDataCallback_other_order = mDataCallback;
	}
    
    public MyAsynaTask(DataCallback_get_other_orders mDataCallback,int code,Map<String, String> map) {
		super();
		// TODO Auto-generated constructor stub
		initMyAsynaTask(code,map);
		this.mDataCallback_get_other_orders = mDataCallback;
	}
    
    public MyAsynaTask(DataCallback_cancel_other_order mDataCallback,int code,Map<String, String> map) {
		super();
		// TODO Auto-generated constructor stub
		initMyAsynaTask(code,map);
		this.mDataCallback_cancel_other_order = mDataCallback;
	}
    
    public MyAsynaTask(DataCallback_get_services mDataCallback,int code,Map<String, String> map) {
		super();
		// TODO Auto-generated constructor stub
		initMyAsynaTask(code,map);
		this.mDataCallback_get_services = mDataCallback;
	}
    
	/**
     * 此方法在后台线程执行，完成任务的主要工作，通常需要较长的时间。在执行过程中可以调用publicProgress(Progress...)来更新任务的进度。
     * @param params
     * @return
     */
    @Override  
    protected String doInBackground(String... params) { 
    	doResult(ConfigUtilHelper.requestOther(Arguments.SERVER_URL_BASE_OTHER, map));
        return null;  
    }  
    @Override  
    protected void onCancelled() {  
        super.onCancelled();  
    } 
	/**
	 * 此方法在主线程执行，任务执行的结果作为此方法的参数返回。
	 * @param result
	 */
    @Override  
    protected void onPostExecute(String result) { 
    	switch (code) {
		case GlobalSetting.XML_GET_OTHER_AREA://获取异地城市列表
			mDataCallback.updateData_CityList(mAreaResultModel);
			break;
		case GlobalSetting.XML_GET_OTHER_HOSPITAL://获取异地城市列表
			mDataCallback.updateData_HospitalList(mHospitalResultModel);
			break;
		case GlobalSetting.XML_GET_OTHER_DEP://获取异地科室列表
			mDataCallback.updateData_DepList(mDepResultModel);
			break;
		case GlobalSetting.XML_GET_OTHER_DOC://获取异地医生列表
			mDataCallback.updateData_DocList(mDocResultModel);
			break;
		case GlobalSetting.XML_GET_OTHER_SCH://获取异地班次列表
			mDataCallback.updateData_ScheduleList(mScheduleResultModel);
			break;
		case GlobalSetting.XML_GET_OTHER_HOS_INFO://获取异地医院详情
			mDataCallback.get_hospital_info(mHospitalInfoModel);
			break;
		case GlobalSetting.XML_GET_OTHER_DOC_INFO://获取异地医生详情
			mDataCallback.get_doc_info(docInfoModel);
			break;
		case GlobalSetting.XML_GENERATION_OTHER_ORDER://异地下单
			mDataCallback_other_order.make_order(orderResultModel);
			break;
		case GlobalSetting.XML_GET_OTHER_ORDERLIST://异地订单列表查询
			mDataCallback_get_other_orders.get_other_orders(orderListResultModel);
			break;
		case GlobalSetting.XML_CANCEL_OTHER_ORDER://异地取消订单
			mDataCallback_cancel_other_order.cancel_other_order(candelOrderResultModel);
			break;
		case GlobalSetting.XML_GET_OTHER_ORDER_INFO://查询异地订单详情
			mDataCallback_cancel_other_order.get_order_info(orderInfoModel);
			break;
		case GlobalSetting.XML_GET_OTHER_SERVICES://查询异地专家门诊的服务列表
			mDataCallback_get_services.get_services(getServicesResultModel);
			break;
		default:
			break;
		}
    	
    }  
    /**
     * 当任务执行之前开始调用此方法
     */
    @Override  
    protected void onPreExecute() {  
    }  
    /**
     * 此方法在主线程执行，用于显示任务执行的进度。
     * @param values
     */
    @Override  
    protected void onProgressUpdate(Integer... values) {  
    }  
    
    /**
     * 处理请求结果函数
     */
    private void doResult(String result){
    	switch (code) {
    	case GlobalSetting.XML_GET_OTHER_AREA://获取异地城市列表
    		mAreaResultModel = new AreaResultModel();
    		try {
				JSONObject json = new JSONObject(result);
				mAreaResultModel.resultCode = json.getString("resultCode");
				mAreaResultModel.resultMsg = json.getString("resultMsg");
				mAreaResultModel.totalCount = json.getInt("totalCount");
				JSONArray array = json.getJSONArray("areas");
				List<AreaModel> areaList = new ArrayList<AreaModel>();
				int length = array.length();
				for (int i = 0; i < length; i++) {
					JSONObject item = (JSONObject) array.opt(i);
					AreaModel a = new AreaModel();
					a.province = item.getString("province");
					a.areaname = item.getString("areaname");
					a.areaid = item.getString("areaid");
					areaList.add(a);
				}
				mAreaResultModel.areaList = areaList;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		break;
    	case GlobalSetting.XML_GET_OTHER_HOSPITAL://获取异地医院列表
    		mHospitalResultModel = new HospitalResultModel();
    		try {
				JSONObject json = new JSONObject(result);
				mHospitalResultModel.resultCode = json.getString("resultCode");
				mHospitalResultModel.resultMsg = json.getString("resultMsg");
				mHospitalResultModel.totalCount = json.getInt("totalCount");
				JSONArray array = json.getJSONArray("hospitals");
				List<HospitalModel> hospitalList = new ArrayList<HospitalModel>();
				int length = array.length();
				for (int i = 0; i < length; i++) {
					JSONObject item = (JSONObject) array.opt(i);
					HospitalModel h = new HospitalModel();
					h.hospitalname = item.getString("hospitalname");
					h.hospitalid = item.getString("hospitalid");
					hospitalList.add(h);
				}
				mHospitalResultModel.hospitalList = hospitalList;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	case GlobalSetting.XML_GET_OTHER_DEP://获取异地科室列表
    		mDepResultModel = new DepResultModel();
    		try {
				JSONObject json = new JSONObject(result);
				mDepResultModel.resultCode = json.getString("resultCode");
				mDepResultModel.resultMsg = json.getString("resultMsg");
				mDepResultModel.totalCount = json.getInt("totalCount");
				JSONArray array = json.getJSONArray("depts");
				List<DepModel> depList = new ArrayList<DepModel>();
				int length = array.length();
				for (int i = 0; i < length; i++) {
					JSONObject item = (JSONObject) array.opt(i);
					DepModel dep = new DepModel();
					dep.deptname = item.getString("deptname");
					dep.deptid = item.getString("deptid");
					depList.add(dep);
				}
				mDepResultModel.depList = depList;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		break;
    	case GlobalSetting.XML_GET_OTHER_DOC://获取异地医生列表
    		mDocResultModel = new DocResultModel();
    		try {
				JSONObject json = new JSONObject(result);
				mDocResultModel.resultCode = json.getString("resultCode");
				mDocResultModel.resultMsg = json.getString("resultMsg");
				mDocResultModel.totalCount = json.getInt("totalCount");
				JSONArray array = json.getJSONArray("doctors");
				List<DocModel> docList = new ArrayList<DocModel>();
				int length = array.length();
				for (int i = 0; i < length; i++) {
					JSONObject item = (JSONObject) array.opt(i);
					DocModel doc = new DocModel();
					doc.doctorId = item.getString("doctorId");
					doc.doctorname = item.getString("doctorname");
					doc.title = item.getString("title");
					doc.outFee = item.getString("outFee");
					doc.outType = item.getString("outType");
					doc.photoUrl = item.getString("photoUrl");
					docList.add(doc);
				}
				mDocResultModel.docList = docList;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		break;
    	case GlobalSetting.XML_GET_OTHER_SCH://获取异地医生排班
    		mScheduleResultModel = new ScheduleResultModel();
    		try {
				JSONObject json = new JSONObject(result);
				mScheduleResultModel.resultCode = json.getString("resultCode");
				mScheduleResultModel.resultMsg = json.getString("resultMsg");
				if(json.has("totalCount")){
					mScheduleResultModel.totalCount = json.getInt("totalCount");
				}
				JSONArray array = json.getJSONArray("worktimes");
				List<ScheduleModel> schList = new ArrayList<ScheduleModel>();
				int length = array.length();
				for (int i = 0; i < length; i++) {
					JSONObject item = (JSONObject) array.opt(i);
					ScheduleModel sch = new ScheduleModel();
					sch.recordId = item.getString("recordId");
					sch.number = item.getInt("number");
					sch.weekDay = item.getString("weekDay");
					sch.healDate = item.getString("healDate");
					sch.dayTime = item.getString("dayTime");
					schList.add(sch);
				}
				mScheduleResultModel.schList = schList;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		break;
    	case GlobalSetting.XML_GET_OTHER_HOS_INFO://获取医院详情
    		mHospitalInfoModel = new HospitalInfoModel();
    		try {
				JSONObject json = new JSONObject(result);
				JSONObject j = json.getJSONObject("hospital");
				mHospitalInfoModel.hospitalid = j.getString("hospitalid");
				mHospitalInfoModel.hospitalname = j.getString("hospitalname");
				mHospitalInfoModel.intro = j.getString("intro");
				mHospitalInfoModel.ride = j.getString("ride");
				mHospitalInfoModel.telephone = j.getString("telephone");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		break;
    	case GlobalSetting.XML_GET_OTHER_DOC_INFO://获取医生详情
    		docInfoModel = new DocInfoModel();
    		try {
				JSONObject json = new JSONObject(result);
				JSONObject j = json.getJSONObject("doctor");
				docInfoModel.doctorname = j.getString("doctorname");
				docInfoModel.title = j.getString("title");
				docInfoModel.doctorId = j.getString("doctorId");
				docInfoModel.photoUrl = j.getString("photoUrl");
				docInfoModel.outType = j.getString("outType");
				docInfoModel.outFee = j.getString("outFee");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		break;
    	case GlobalSetting.XML_GENERATION_OTHER_ORDER://异地下单
    		orderResultModel = new GeneraOrderResultModel();
    		try{
    			JSONObject json = new JSONObject(result);
    			orderResultModel.resultCode = json.getString("resultCode");
    			orderResultModel.resultMsg = json.getString("resultMsg");
    		}catch (Exception e) {
				// TODO: handle exception
			}
    		break;
    	case GlobalSetting.XML_GET_OTHER_ORDERLIST://异地订单列表
    		orderListResultModel = new OrderListResultModel();
    		try {
    			JSONObject json = new JSONObject(result);
    			orderListResultModel.resultCode = json.getString("resultCode");
    			orderListResultModel.resultMsg = json.getString("resultMsg");
    			orderListResultModel.totalCount = json.getInt("totalCount");
				JSONArray array = json.getJSONArray("orders");
				List<OrderModel> orderList = new ArrayList<OrderModel>();
				int length = array.length();
				for (int i = 0; i < length; i++) {
					JSONObject item = (JSONObject) array.opt(i);
					OrderModel order = new OrderModel();
					order.userName = item.getString("userName");
					order.updateTime = item.getString("updateTime");
					order.mobile = item.getString("mobile");
					order.orderType = item.getString("orderType");
					order.orderTime = item.getString("orderTime");
					order.userCardNo = item.getString("userCardNo");
					order.orderNo = item.getString("orderNo");
					order.sex = item.getString("sex");
					if(item.has("orderFee")){
						order.orderFee = item.getString("orderFee");
					}
					order.bestContactTime = item.getString("bestContactTime");
					order.orderStatus = item.getString("orderStatus");
					order.payStatus = item.getString("payStatus");
					order.birth = item.getString("birth");
					order.orderDesc = item.getString("orderDesc");
					order.firstorderDesc = item.getString("firstorderDesc");
					order.reservationType = item.getString("reservationType");
					order.serviceSumPrice = item.getString("serviceSumPrice");
					order.isFastReservation = item.getString("isFastReservation");
					order.doctorId = item.getString("doctorId");
					order.hospitalid = item.getString("hospitalid");
					order.deptId = item.getString("deptId");
					orderList.add(order);
				}
				orderListResultModel.orderList = orderList;
			} catch (Exception e) {
				// TODO: handle exception
			}
    		break;
    	case GlobalSetting.XML_CANCEL_OTHER_ORDER://取消订单
    		candelOrderResultModel = new CandelOrderResultModel();
    		try {
    			JSONObject json = new JSONObject(result);
    			candelOrderResultModel.resultCode = json.getString("resultCode");
    			candelOrderResultModel.resultMsg = json.getString("resultMsg");
			} catch (Exception e) {
				// TODO: handle exception
			}
    		break;
    	case GlobalSetting.XML_GET_OTHER_ORDER_INFO://异地订单详情
    		orderInfoModel = new OrderInfoModel();
    		OrderModel order = new OrderModel();
    		List<ServiceInfoModel> mServiceInfoModelList = new ArrayList<ServiceInfoModel>();
    		try {
    			JSONObject json = new JSONObject(result);
    			orderInfoModel.resultCode = json.getString("resultCode");
    			orderInfoModel.resultMsg = json.getString("resultMsg");
    			JSONObject item = json.getJSONObject("order");
    			order.userName = item.getString("userName");
				order.updateTime = item.getString("updateTime");
				order.mobile = item.getString("mobile");
				order.orderType = item.getString("orderType");
				order.orderTime = item.getString("orderTime");
				order.userCardNo = item.getString("userCardNo");
				order.orderNo = item.getString("orderNo");
				order.sex = item.getString("sex");
				if(item.has("orderFee")){
					order.orderFee = item.getString("orderFee");
				}
				order.bestContactTime = item.getString("bestContactTime");
				order.orderStatus = item.getString("orderStatus");
				order.payStatus = item.getString("payStatus");
				order.birth = item.getString("birth");
				order.orderDesc = item.getString("orderDesc");
				order.firstorderDesc = item.getString("firstorderDesc");
				
				order.reservationType = item.getString("reservationType");
				order.serviceSumPrice = item.getString("serviceSumPrice");
				order.isFastReservation = item.getString("isFastReservation");
				order.doctorId = item.getString("doctorId");
				order.hospitalid = item.getString("hospitalid");
				order.deptId = item.getString("deptId");
				JSONArray array = item.getJSONArray("serviceInfo");
				for (int i = 0; i < array.length(); i++) {
					JSONObject service = (JSONObject) array.opt(i);
					ServiceInfoModel mServiceInfoModel = new ServiceInfoModel();
					if(service.has("serviceName")){
						mServiceInfoModel.serviceName = service.getString("serviceName");
					}
					if(service.has("serviceCode")){
						mServiceInfoModel.serviceCode = service.getString("serviceCode");
					}
					if(service.has("serviceContent")){
						mServiceInfoModel.serviceContent = service.getString("serviceContent");
					}
					if(service.has("servicePrice")){
						mServiceInfoModel.servicePrice = service.getString("servicePrice");
					}
					mServiceInfoModelList.add(mServiceInfoModel);
				}
				order.mServiceInfoModelList = mServiceInfoModelList;
				orderInfoModel.mOrderModel = order;
			} catch (Exception e) {
				// TODO: handle exception
			}
    		break;
    	case GlobalSetting.XML_GET_OTHER_SERVICES://查询异地专家门诊的服务列表
    		getServicesResultModel = new GetServicesResultModel();
    		
    		try {
    			JSONObject json = new JSONObject(result);
    			getServicesResultModel.resultCode = json.getString("resultCode");
    			getServicesResultModel.resultMsg = json.getString("resultMsg");
    			JSONArray array = json.getJSONArray("serviceInfos");
    			List<ServiceInfoModel> infos = new ArrayList<ServiceInfoModel>();
    			int length = array.length();
				for (int i = 0; i < length; i++) {
					JSONObject item = (JSONObject) array.opt(i);
					ServiceInfoModel info = new ServiceInfoModel();
					info.serviceCode = item.getString("serviceCode");
					info.serviceName = item.getString("serviceName");
					info.serviceContent = item.getString("serviceContent");
					info.servicePrice = item.getString("servicePrice");
					infos.add(info);
				}
				getServicesResultModel.serviceInfos = infos;
			} catch (Exception e) {
				// TODO: handle exception
			}
    		break;
		default:
			break;
    	}
    }
    
    
}